package com.mp.ipm2020.mbg.util;

import com.mp.ipm2020.mbg.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther adidasshe@mp.cn
 * <p>
 * 解析mqtt收发数据格式，方便存储跟下发指令
 */
@Component
public class ResolverDeviceDataUtil {

    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * 解析mqtt接收的消息
     *
     * @param data
     */
    public HashMap<String,String> resolver(String data) {
        Pattern pattern = Pattern.compile("(.*)@(.*)(\\{.*\\})");
        Matcher matcher = pattern.matcher(data);
        HashMap<String, String> map = new HashMap<>();
        if (matcher.find()) {
            map.put("deviceId",matcher.group(1));
            map.put("type",matcher.group(2));
            map.put("data",matcher.group(3));
            return map;
        }
        return null;
    }


}